A method, medium and device for generating an SQL statement based on a large language model
By combining keyword extraction, vector transformation, and similarity filtering techniques in a large language model, the problem of generating accurate SQL statements for non-professional users has been solved, achieving higher generation accuracy and SQL statements that match user intent.
Patent Information
- Application Number
- CN202411969390.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2044-12-30
AI Technical Summary
In existing technologies, non-professional users find it difficult to generate accurate SQL statements using natural language, and large language models lack effective use of historical experience when faced with massive amounts of historical data and different business scenarios, resulting in poor accuracy in generating SQL statements.
By acquiring historical input databases, initial statements input by target users, and preset keyword configuration tables, keywords are extracted and matched. Combined with vector transformation models and similarity filtering, SQL statements are generated using large language models to ensure accuracy.
It improves the accuracy of SQL statement generation, avoids misinterpretation of user intent by large language models, and generates SQL statements that better meet user needs.
Smart Images

Figure CN119862200B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing, and in particular to a method, medium, and device for generating SQL statements based on a large language model. Background Technology
[0002] In today's digital age, databases play a central role in information management and data processing across numerous fields. However, for most non-professional database users, writing SQL statements is a challenging task due to the strict and complex requirements of SQL syntax, the need for familiarity with database structure, various operation commands, and the relationships between data.
[0003] With the continuous development of natural language processing technology, especially the powerful language understanding and generation capabilities of large language models, users expect to be able to easily generate corresponding SQL statements from natural language to lower the barrier to database use and improve the efficiency of data acquisition and operation. However, relying solely on large language models to directly convert natural language into SQL statements has certain limitations. For example, the diversity and ambiguity of natural language may lead to biases in model understanding. Moreover, when faced with massive historical data and different business scenarios, there is a lack of effective mechanisms to utilize historical experience and specifically handle certain keywords to improve the accuracy and efficiency of SQL statement generation, resulting in poor accuracy of generated SQL statements.
[0004] Therefore, improving the accuracy of SQL statement generation results has become an urgent problem to be solved. Summary of the Invention
[0005] To address the aforementioned technical problems, the present invention provides a method for generating SQL statements based on a large language model. This method includes the following steps:
[0006] S10: Obtain the historical input database, the initial statement input by the target user, the target scenario and target table corresponding to the initial statement, and the target vector transformation model and preset keyword configuration table corresponding to the target scenario. The historical input database includes several historical statements, the historical statement vector and historical SQL statement corresponding to each historical statement, and the preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0007] S20: Extract keywords from the initial statement to obtain several target keywords corresponding to the initial statement.
[0008] S30, match each target keyword with the preset keyword configuration table to obtain the matching result corresponding to each target keyword. The matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression.
[0009] S40: Based on the matching results of all target keywords, obtain several target regular expressions corresponding to the initial statement.
[0010] S50: Obtain the statement vector corresponding to the initial statement based on the target vector transformation model corresponding to the target scenario of the initial statement.
[0011] S60: Based on the statement vector and the historical statement vector corresponding to each historical statement, obtain the similarity between the initial statement and each historical statement.
[0012] S70, based on the comparison between the similarity between the initial statement and each historical statement and a preset similarity threshold, select several reference statements corresponding to the initial statement from all historical statements.
[0013] S80: Input the statement vector, target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0014] The present invention also provides a non-transitory computer-readable storage medium storing at least one instruction or at least one program, wherein the at least one instruction or at least one program is loaded and executed by a processor to implement the above-described SQL statement generation method based on a large language model.
[0015] The present invention also provides an electronic device, including a processor and the aforementioned non-transitory computer-readable storage medium.
[0016] The present invention has at least the following beneficial effects: by extracting keywords from the initial statement and matching them with a preset keyword configuration table to obtain the target regular expression, it can accurately capture key semantic information in natural language, standardize fuzzy and diverse natural language expressions according to established business rules, avoid the large language model's misinterpretation of user intent, and at the same time, by selecting reference statements from numerous historical statements that are highly similar to the initial statement and have reference value for the generation of current SQL statements, it helps the preset large language model generate more accurate SQL statements that conform to user intent based on similar situations in the past, thereby improving the accuracy of SQL statement generation. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 A flowchart illustrating a method for generating SQL statements based on a large language model, provided in Embodiment 1 of the present invention;
[0019] Figure 2 This is a schematic diagram of the structure of an SQL statement generation device based on a large language model provided in Embodiment 2 of the present invention. Detailed Implementation
[0020] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0021] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention 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 terms used to distinguish similar objects can be interchanged so that the invention can also be implemented in other embodiments besides the illustrated or described embodiments. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. 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 explicitly listed, but may include other steps or units not explicitly listed or inherent to these processes, methods, products, or devices.
[0022] Example 1
[0023] This embodiment provides a method for generating SQL statements based on a large language model. The method includes the following steps: Figure 1 As shown:
[0024] S10: Obtain the historical input database, the initial statement input by the target user, the target scenario and target table corresponding to the initial statement, and the target vector transformation model and preset keyword configuration table corresponding to the target scenario. The historical input database includes several historical statements, the historical statement vector and historical SQL statement corresponding to each historical statement, and the preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0025] The initial statement input by the user is their data manipulation requirement expressed in natural language. The target scenario is a classification and definition of the business or operation type within which the initial statement falls. The target table refers to the specific data table in the database related to the user's requirement expressed through the initial statement.
[0026] For example, target scenarios can be categorized by business area into sales, finance, and human resources scenarios. They can also be categorized by operation type into query, insert, and update scenarios. In an enterprise management database, target tables might include employee information tables, sales data tables, and department information tables. The description text for the employee information table might state that "this table contains fields such as employee ID, name, department, and date of employment. The employee ID is the primary key, uniquely identifying each employee," etc.
[0027] The role of vector transformation models is to convert natural language text into vector representations. Because the characteristics of language and data relationships vary across different pre-defined scenarios, a suitable vector transformation model is needed to more accurately measure the relationship between natural language text and its vector representation, providing a suitable vector data foundation for subsequent SQL statement generation.
[0028] The historical input database is a collection that stores a large amount of data related to past database interactions. It contains several historical statements, each with a corresponding historical statement vector and a corresponding historical SQL statement. The historical statement vector is a digital representation of the historical statement, which is easy for computers to process and contains semantic information from the historical statement. The historical SQL statement is SQL code that was successfully generated and executed based on the corresponding historical statement and conforms to the database syntax specifications.
[0029] The preset keyword configuration table is a pre-defined table containing several custom keywords and their corresponding regular expressions. Custom keywords are important terms extracted from a business perspective, possessing specific meanings and database operation implications, such as "onboarding date" and "offboarding date" in a human resources management context. Regular expressions are tools used to match and standardize text formatting. The regular expression corresponding to each custom keyword specifies how to accurately identify the keyword and its related expressions in natural language. For example, the regular expression for "onboarding date" might be "onboarding\s+[\s+] date," used to match statements like "onboarding on January 1, 2024." This method accurately extracts and standardizes key information from initial statements and other text, aiding in determining the logic for generating subsequent SQL statements.
[0030] In one specific embodiment, S10 includes the following steps:
[0031] S101, obtain the initial vector transformation model, description text samples corresponding to several preset tables, preset scenarios and description vector samples corresponding to each description text sample, and preset sampling ratios corresponding to each preset scenario.
[0032] S102, set the preset sampling ratio as the target sampling ratio, set each preset scene as the initial scene, and initialize the number of repetitions N=1.
[0033] S103, sample from several descriptive text samples according to the target sampling ratio corresponding to each initial scene, and obtain the Nth descriptive text training sample set, the Nth descriptive text verification sample set, and the Nth number of descriptive text samples corresponding to each initial scene in the Nth descriptive text training sample set.
[0034] S104. Based on the Nth descriptive text training sample set, the Nth descriptive text verification sample set, and the descriptive vector sample corresponding to each descriptive text sample, train the initial vector conversion model to obtain the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model.
[0035] S105, based on the Nth quantity and Nth accuracy corresponding to each initial scene, obtain the Nth degree of fit between each initial scene and the Nth vector transformation model.
[0036] S106. For any initial scene, determine the adaptation result corresponding to the current initial scene based on the comparison result between the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold. The adaptation result includes adaptation, non-adaptation, and undetermined.
[0037] S107, if the adaptation result corresponding to the current initial scene is an adaptation, then the current initial scene is determined as the first scene, and the Nth vector transformation model is determined as the target vector transformation model corresponding to the current first scene.
[0038] S108, if the adaptation result corresponding to the current initial scene is not compatible, then the current initial scene is determined as the second scene.
[0039] S109, if the adaptation result corresponding to the current initial scene is to be judged, then the current initial scene is determined as the third scene.
[0040] S110, traverse all initial scenes, obtain all first scenes and the target vector transformation model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scenes.
[0041] S111, based on the preset scenario and description vector sample corresponding to each description text sample, obtain the target description text sample set corresponding to each second scenario.
[0042] S113, Based on the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, train the initial vector conversion model to obtain the target vector conversion model corresponding to each second scene.
[0043] 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 transformation model corresponding to each preset scene.
[0044] S115, Based on the target vector transformation model corresponding to each preset scenario, obtain the target vector transformation model corresponding to the target scenario.
[0045] The descriptive text samples are used to explain in detail the structure, field meanings, data ranges, and other information of each preset table, which helps to understand the specific composition of the table so as to accurately construct the relationship between SQL statements and tables.
[0046] The initial vector transformation model is a general vector transformation model that is not optimized for specific scenarios. In the future, we will train and adjust it based on the initial vector transformation model and specific data so that it can adapt to the text-to-vector transformation needs of different preset scenarios.
[0047] Description vector samples are the correct vector representations of corresponding description text samples, used to guide the model in learning how to accurately convert description text into vectors. Description vector samples can be obtained by converting description text samples using known vector transformation techniques. Those skilled in the art will recognize that any existing vector transformation technique falls within the scope of this invention, and will not be elaborated upon here. For example, the vector transformation technique could be Word2Vec.
[0048] During the training of the initial vector transformation model, descriptive text samples from the Nth descriptive text training sample set are input into the initial vector transformation model to help it learn how to convert descriptive text samples into vectors. Then, descriptive text samples from the Nth descriptive text validation sample set are used for validation, comparing the differences between the model's output vector and the corresponding descriptive vector sample. Evaluation metrics such as cosine similarity are used to measure the model's accuracy in each initial scenario. Finally, the Nth vector transformation model and its Nth accuracy for each initial scenario are obtained, reflecting the accuracy of the current model in converting text to vectors in the corresponding initial scenario.
[0049] For any initial scenario, the corresponding Nth fit level is compared with a pre-set threshold. If the Nth fit level is higher than the threshold, it indicates that the Nth vector transformation model performs well in this initial scenario and has an ideal fit. If it is lower than the threshold, the Nth vector transformation model is considered to be poorly suited to the initial scenario. If it is near the threshold or in other uncertain situations, it is classified as pending evaluation, requiring further updates and matching of the vector transformation model adapted to the initial scenario.
[0050] For cases identified as the first scenario, a target vector transformation model is determined for each first scenario in order to complete the determination of suitable models for some scenarios.
[0051] For cases identified as the second scenario, a set of target description text samples suitable for retraining is collected and organized. The initial vector transformation model corresponding to each second scenario is trained separately to obtain the target vector transformation model corresponding to each second scenario.
[0052] For cases identified as the third scenario, the vector transformation model is continuously optimized by updating the sampling ratio and repeating training until all preset scenarios have clear model adaptation judgment results, and finally the target vector transformation model corresponding to each preset scenario is obtained.
[0053] The method described above, which uses multiple iterations of training and evaluation to find suitable vector transformation models for different preset scenarios, aims to improve the accuracy and effectiveness of subsequent SQL statement transformations, enabling them to better adapt to diverse business scenarios and natural language input.
[0054] By acquiring basic data including initial vector conversion models, descriptive text samples and related preset scenarios and descriptive vector samples, and preset sampling ratios, and through multiple rounds of iterative operations, the preset scenarios and related sample data and vector conversion models are sampled, trained, evaluated, and classified. Each initial scenario is divided into a first-fit scenario and its target vector conversion model is determined; a second-fit scenario is to be retrained; and a third-fit scenario is to be further analyzed and optimized, until all preset scenarios have obtained a suitable target vector conversion model. This lays a precise and highly adaptable vector conversion foundation for subsequent natural language and SQL statement conversion based on the target vector conversion model, improves the depth and accuracy of understanding the initial statements corresponding to each scenario during 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.
[0055] In one specific implementation, the preset sampling ratio corresponding to each preset scenario includes the training sample sampling ratio and the validation sample sampling ratio. S103 includes the following steps:
[0056] S1031, For any initial scene, according to the sampling ratio of the training samples corresponding to the current initial scene, sample a number of descriptive text samples from all descriptive text samples corresponding to the current initial scene as training samples, and obtain the Nth number of descriptive text samples used as training samples corresponding to the current initial scene.
[0057] S1032, Based on the sampling ratio of the verification samples corresponding to the current initial scenario, sample a number of description text samples from all description text samples corresponding to the current initial scenario as verification samples.
[0058] S1033, traverse all initial scenes and obtain several descriptive text samples as training samples and several descriptive text samples as verification samples corresponding to each initial scene.
[0059] S1034, determine the set of several descriptive text samples corresponding to all initial scenes as training samples as the Nth descriptive text training sample set.
[0060] S1035, determine the set of several descriptive text samples corresponding to all initial scenarios as verification samples as the Nth descriptive text verification sample set.
[0061] As described above, training samples and validation samples are reasonably divided from the descriptive text samples of each initial scenario according to the corresponding sampling ratio, and finally the overall training sample set and validation sample set are constructed. This provides a key data preparation and organizational foundation for subsequent training, validation and gradual optimization of the initial vector conversion model based on the training sample set and validation 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.
[0062] In one specific embodiment, S105 includes the following steps:
[0063] S1051, normalize the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene.
[0064] S1052, input the normalized Nth quantity and Nth accuracy corresponding to each initial scene into the preset adaptation degree calculation model to obtain the Nth adaptation degree between each initial scene and the Nth vector transformation model. The Nth adaptation degree is inversely proportional to the corresponding normalized Nth quantity and directly proportional to the corresponding Nth accuracy.
[0065] The number of descriptive text samples extracted as training samples from different initial scenarios may vary significantly. This embodiment uses normalization to convert the quantity values of different magnitudes into a standardized and comparable range, creating conditions for accurate measurement of fit in the future. The value range of the Nth quantity after normalization is [0, 1]. Those skilled in the art will know that any normalization method with a value range of [0, 1] in the prior art falls within the protection scope of this invention, and will not be elaborated further here. For example, the normalization method in this embodiment can be the Min-Max normalization method.
[0066] When the normalized Nth element corresponding to the initial scene is large, it indicates that there is relatively abundant training data for the vector conversion model to learn the text conversion patterns related to that initial scene. Correspondingly, a large amount of data should help the vector conversion model better capture patterns and improve accuracy. However, when the Nth element 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, resulting in a significant deviation between the generated vector and the expected vector. Therefore, when the normalized Nth element corresponding to the initial scene is large but the Nth element accuracy is low, it indicates 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 descriptive text samples and corresponding descriptive vector samples under that initial scene. Therefore, the degree of Nth element fit is inversely proportional to the corresponding normalized Nth element, and the degree of Nth element fit is directly proportional to the corresponding Nth element accuracy.
[0067] The preset adaptation degree calculation model is a weighted summation model. The implementer can set the first weight corresponding to the Nth quantity after normalization and the second weight corresponding to the Nth accuracy according to the actual situation. By performing a weighted summation on the Nth quantity and the Nth accuracy after normalization, the Nth adaptation degree between each initial scene and the Nth vector transformation model can be obtained.
[0068] The above-mentioned normalization process ensures that the number of training samples can participate in the evaluation on a fair and comparable basis. Then, based on the preset relationship between the degree of fit and the Nth number and the Nth accuracy after normalization, the degree of fit is calculated using the degree of fit calculation model. This provides a scientific and quantitative basis for accurately judging the fit between each initial scenario and the vector conversion model in the entire process. It helps to improve the accuracy and reliability of building vector conversion models that fit different preset scenarios, thereby improving the accuracy of subsequent SQL statement conversion.
[0069] In one specific embodiment, the preset degree threshold includes a first degree threshold and a second degree threshold, and S106 includes the following steps:
[0070] S1061, if the Nth degree of adaptation corresponding to the current initial scene is less than or equal to the first degree threshold, then the adaptation result corresponding to the current initial scene is determined to be unsuitable.
[0071] S1062, if the Nth degree of adaptation corresponding to the current initial scene is greater than or equal to the second degree threshold, then it is determined that the adaptation result corresponding to the current initial scene is not suitable.
[0072] S1063, if the Nth degree of adaptation corresponding to the current initial scene is less than the second degree threshold but greater than the first degree threshold, then the adaptation result corresponding to the current initial scene is determined to be pending judgment.
[0073] If the Nth fit 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 transformation model is very poor in terms of sample utilization, scene semantic understanding accuracy, and ability to generate vectors that meet expectations in the current initial scene. In this case, it can be clearly determined that the fit result corresponding to the current initial scene is not suitable. That is, the vector transformation model trained in multiple initial scenes is not suitable for the current initial scene. Then, a target description text sample set suitable for retraining can be collected and organized, and the initial vector transformation model corresponding to each second scene can be trained separately to obtain a suitable target vector transformation model.
[0074] If the Nth degree of fit corresponding to the current initial scenario is greater than or equal to the set second degree threshold, it means that the Nth vector transformation model has reached the ideal fit state in the current initial scenario, can effectively convert the initial statement in the current initial scenario into a suitable vector, and performs well in terms of accuracy, etc., then the current Nth vector transformation model can be used as the target vector transformation model for the current initial scenario.
[0075] If the Nth degree of fit corresponding to the current initial scene is less than the second degree threshold but greater than the first degree threshold, then the fit result corresponding to the current initial scene is determined to be pending, and more rounds of analysis and processing are needed to determine the fit between the current initial scene and the current vector transformation model.
[0076] The specific values of the first-level threshold and the second-level threshold can be set by the implementer according to the actual situation.
[0077] As described above, by setting a first-degree threshold and a second-degree threshold, three types of adaptation results—unsuitable, suitable, and pending judgment—are clearly and reasonably distinguished. This provides clear judgment criteria and classification basis for the evaluation and selection of vector conversion models under different initial scenarios. It enables the systematic classification and processing of numerous initial scenarios and the adoption of corresponding optimization strategies for different adaptation results. This 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.
[0078] In one specific embodiment, S10 further includes the following steps:
[0079] S116, obtain several preset tables, preset description text and preset scenarios corresponding to each preset table, and target vector transformation model corresponding to each preset scenario.
[0080] S117, Perform scenario consistency matching on the preset scenario corresponding to each preset table and the target scenario corresponding to the initial statement, and obtain the scenario consistency matching result between each preset table and the initial statement. The scenario consistency matching result includes scenario consistency and scenario inconsistency.
[0081] S118, each preset table whose scenario consistency matching result is consistent with the scenario is determined as the intermediate table corresponding to the initial statement.
[0082] S119. Based on the target vector transformation model corresponding to the preset scenario corresponding to the preset description text of each intermediate table, obtain the intermediate description vector corresponding to each intermediate table.
[0083] S120: Obtain the statement vector corresponding to the initial statement based on the target vector transformation model corresponding to the target scenario of the initial statement.
[0084] S121, based on the intermediate description vector corresponding to each intermediate table and the statement vector corresponding to the initial statement, obtain the intermediate matching degree between the initial statement and each intermediate table.
[0085] S122, Based on the degree of intermediate matching between the initial statement and each intermediate table, filter out the target table corresponding to the initial statement from all intermediate tables.
[0086] Specifically, by comparing the preset scenarios corresponding to the preset tables with the target scenarios corresponding to the initial statements, it is determined whether they match at the business or operation type level. This determines the scenario consistency between the preset tables and the initial statements, providing a basis for selecting relevant tables that match the scenario of the initial statements and avoiding the introduction of tables that do not meet the user's needs into subsequent calculations and analyses.
[0087] By calculating the similarity between the intermediate description vector corresponding to each intermediate table and the statement vector corresponding to the initial statement, we can measure the degree of association between the initial statement and each intermediate table, thereby obtaining the intermediate matching degree between the initial statement and each intermediate table. This serves as the basis for accurately selecting the target table that best matches the initial statement from among many intermediate tables, and the intermediate table corresponding to the highest intermediate matching degree is determined as the target table corresponding to the initial statement, thereby improving the accuracy and rationality of the generated SQL statement.
[0088] The above describes a process where, by first collecting relevant information from a preset table and performing scenario matching to filter out intermediate tables, then converting the description text of the intermediate tables and the initial statements into vectors and calculating the degree of matching, the target table is finally selected. This provides a crucial operational object foundation that closely matches user needs for the subsequent accurate generation of SQL statements using a large language model, ensuring the accuracy and rationality of the entire SQL statement generation method and enabling it to better serve users' needs to convert natural language into SQL statements for database operations.
[0089] In one specific embodiment, S10 further includes the following steps:
[0090] S123, obtain several historical statements and the preset scenario corresponding to each historical statement.
[0091] S124. Based on the target vector transformation model corresponding to the preset scenario for each historical statement, obtain the historical statement vector corresponding to each historical statement.
[0092] S20: Extract keywords from the initial statement to obtain several target keywords corresponding to the initial statement.
[0093] Those skilled in the art will know that any keyword extraction method in the prior art falls within the protection scope of this invention, and will not be elaborated further here.
[0094] As described above, several target keywords are extracted from the initial statement input by the user, simplifying the complex statement into a set of words that can reflect the core semantics and key elements. This allows for more accurate matching, analysis, and guidance of the SQL statement generation logic, thereby improving the relevance of the final generated SQL statement to the user's true intent.
[0095] S30, match each target keyword with the preset keyword configuration table to obtain the matching result corresponding to each target keyword. The matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression.
[0096] In one specific embodiment, S30 further includes the following steps:
[0097] S301, for any target keyword, perform word consistency matching between the current target keyword and each custom keyword in the preset keyword configuration table, and obtain the word consistency matching result between the current target keyword and each custom keyword. The word consistency matching result includes word consistency and word inconsistency.
[0098] S302, for any custom keyword, if the word consistency matching result between the current target keyword and the current custom keyword is consistent, then the set consisting of the current custom keyword and the regular expression corresponding to the current custom keyword is determined as the matching result corresponding to the current target keyword.
[0099] S303: Iterate through all custom keywords. If the word consistency matching results between the current target keyword and all custom keywords are inconsistent, then the empty set is determined as the matching result corresponding to the current target keyword.
[0100] The above method compares the target keywords with the custom keywords in the preset keyword configuration table one by one to determine the matching degree of the target keywords and custom keywords at the word level. Based on this, the matching results corresponding to each target keyword are obtained, which lays a solid foundation for the subsequent integration of the matching results of all target keywords and further guides the SQL statement generation logic based on these results, thereby improving the accuracy and standardization of SQL statement generation.
[0101] S40: Based on the matching results of all target keywords, obtain several target regular expressions corresponding to the initial statement.
[0102] As described above, regular expressions are used to standardize and reflect the text format and matching rules of key semantic information in the initial statement as a whole. This provides a clear pattern basis for the accurate processing of relevant semantic elements in the subsequent SQL statement generation process, and helps to accurately convert the initial statement into an SQL statement according to the established business logic.
[0103] S50: Obtain the statement vector corresponding to the initial statement based on the target vector transformation model corresponding to the target scenario of the initial statement.
[0104] S60: Based on the statement vector and the historical statement vector corresponding to each historical statement, obtain the similarity between the initial statement and each historical statement.
[0105] The above method calculates the similarity between the initial statement vector and the historical statement vector corresponding to each historical statement, quantitatively measuring the closeness between the initial statement and each historical statement in terms of semantics and data relevance. This allows for the subsequent selection of historical statements that are valuable for generating the SQL statement corresponding to the current initial statement based on the similarity, thereby improving the accuracy of SQL statement generation.
[0106] S70, based on the comparison between the similarity between the initial statement and each historical statement and a preset similarity threshold, select several reference statements corresponding to the initial statement from all historical statements.
[0107] Among them, historical statements with a similarity greater than a preset similarity threshold are identified as several reference statements corresponding to the initial statement.
[0108] As described above, based on the comparison results of the similarity between the initial statement and each historical statement and the preset similarity threshold, reference statements with high similarity to the initial statement and which are of reference value for the generation of the current SQL statement are selected from a large number of historical statements. This helps the preset large language model to generate more accurate SQL statements that meet the user's intent based on similar situations in the past, thereby improving the accuracy of SQL statement generation.
[0109] S80: Input the statement vector, target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0110] The above-mentioned method inputs the statement vector, target table, several target regular expressions corresponding to the initial statement, several selected reference statements, and the historical SQL statements corresponding to each reference statement into the preset large language model. By leveraging the powerful language understanding and generation capabilities of the large language model, and comprehensively considering the semantic, structural, and historical information contained in the input content, it generates SQL statements that conform to the user's initial statement intent, thereby realizing the conversion from natural language to database executable statements and improving the accuracy of SQL statement generation.
[0111] The above process involves extracting keywords from the initial statement to obtain several target keywords. Each target keyword is then matched against a preset keyword configuration table to obtain matching results. Based on the matching results of all target keywords, several target regular expressions corresponding to the initial statement are obtained. Using the target vector transformation model corresponding to the target scenario of the initial statement, a statement vector corresponding to the initial statement is obtained. Based on the statement vector and the historical statement vectors corresponding to each historical statement, the similarity between the initial statement and each historical statement is obtained. By comparing the similarity between the initial statement and each historical statement with a preset similarity threshold, several reference statements corresponding to the initial statement are selected from all historical statements. Finally, the statement vectors, target tables, and initial... Several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement are input into a preset large language model to obtain the SQL statement corresponding to the initial statement. By extracting keywords from the initial statement and matching them with a preset keyword configuration table, the target regular expression can be obtained. This can accurately capture key semantic information in natural language, standardize fuzzy and diverse natural language expressions according to established business rules, and avoid the large language model's misinterpretation of user intent. At the same time, by selecting reference statements with high similarity to the initial statement from numerous historical statements and which are of reference value for the generation of the current SQL statement, the preset large language model can generate more accurate SQL statements that conform to user intent based on similar situations in the past, thereby improving the accuracy of SQL statement generation.
[0112] Example 2
[0113] This second embodiment provides an SQL statement generation device based on a large language model. The SQL statement generation device based on a large language model includes, for example: Figure 2 As shown:
[0114] The data acquisition module 21 is used to acquire the historical input database, the initial statement input by the target user, the target scenario and target table corresponding to the initial statement, as well as the target vector transformation model and preset keyword configuration table corresponding to the target scenario. The historical input database includes several historical statements, the historical statement vector corresponding to each historical statement and the historical SQL statement. The preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0115] The keyword extraction module 22 is used to extract keywords from the initial statement and obtain several target keywords corresponding to the initial statement.
[0116] The matching result acquisition module 23 is used to match each target keyword with the preset keyword configuration table and obtain the matching result corresponding to each target keyword. The matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression.
[0117] The target regular expression acquisition module 24 is used to obtain several target regular expressions corresponding to the initial statement based on the matching results of all target keywords.
[0118] The statement vector acquisition module 25 is used to obtain the statement vector corresponding to the initial statement based on the target vector conversion model corresponding to the target scenario corresponding to the initial statement.
[0119] The similarity acquisition module 26 is used to obtain the similarity between the initial statement and each historical statement based on the statement vector and the historical statement vector corresponding to each historical statement.
[0120] The statement filtering module 27 is used to filter out several reference statements corresponding to the initial statement from all historical statements based on the comparison between the initial statement and each historical statement and a preset similarity threshold.
[0121] The SQL statement generation module 28 is used to input the statement vector, target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0122] In one specific embodiment, the data acquisition module 21 includes:
[0123] The first data acquisition submodule is used to acquire the initial vector transformation model, description text samples corresponding to several preset tables, preset scenarios and description vector samples corresponding to each description text sample, and preset sampling ratios corresponding to each preset scenario.
[0124] 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.
[0125] The first training data acquisition submodule is used to sample from several descriptive text samples according to the target sampling ratio corresponding to each initial scene, and obtain the Nth descriptive text training sample set, the Nth descriptive text verification sample set, and the Nth number of descriptive text samples corresponding to each initial scene in the Nth descriptive text training sample set.
[0126] The first model training submodule is used to train the initial vector transformation model based on the Nth descriptive text training sample set, the Nth descriptive text verification sample set, and the descriptive vector sample corresponding to each descriptive text sample, so as to obtain the Nth vector transformation model and the Nth accuracy of each initial scene in the Nth vector transformation model.
[0127] The adaptation degree acquisition submodule is used to obtain the Nth degree of adaptation between each initial scene and the Nth vector transformation model based on the Nth number and Nth accuracy corresponding to each initial scene.
[0128] The adaptation result acquisition submodule is used to determine the adaptation result corresponding to the current initial scene based on the comparison result of the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold for any initial scene. The adaptation result includes adapted, unsuitable, and undetermined.
[0129] 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 an adaptation, and to determine the Nth vector transformation model as the target vector transformation model corresponding to the current first scene.
[0130] The second scenario determination submodule is used to determine the current initial scenario as the second scenario if the adaptation result corresponding to the current initial scenario is not compatible.
[0131] 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 judged.
[0132] The initial scene traversal submodule is used to traverse all initial scenes, obtain all first scenes and the target vector transformation model corresponding to each first scene, all second scenes, all third scenes, and the number of scenes corresponding to each third scene.
[0133] The second training data acquisition submodule is used to obtain the target description text sample set corresponding to each second scene based on the preset scene and description vector sample corresponding to each description text sample.
[0134] The second model training submodule is used to train the initial vector transformation model based on the target description text sample set corresponding to each second scenario and the description vector sample corresponding to each description text sample, so as to obtain the target vector transformation model corresponding to each second scenario.
[0135] The data update 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 transformation model corresponding to each preset scene.
[0136] In one specific implementation, the preset sampling ratio corresponding to each preset scenario includes a training sample sampling ratio and a validation sample sampling ratio. The first training data acquisition submodule includes:
[0137] The training sample acquisition unit is used to sample a number of descriptive text samples from all descriptive text samples corresponding to the current initial scene as training samples, based on the sampling ratio of training samples corresponding to the current initial scene, and obtain the Nth number of descriptive text samples used as training samples corresponding to the current initial scene.
[0138] The verification sample acquisition unit is used to sample a number of descriptive text samples from all descriptive text samples corresponding to the current initial scene as verification samples, based on the verification sample sampling ratio corresponding to the current initial scene.
[0139] The scene traversal unit is used to traverse all initial scenes and obtain several descriptive text samples as training samples and several descriptive text samples as validation samples for each initial scene.
[0140] The training sample set determination unit is used to determine the set of several descriptive text samples corresponding to all initial scenes as training samples as the Nth descriptive text training sample set.
[0141] The verification sample set determination unit is used to determine the set of several descriptive text samples corresponding to all initial scenarios as verification samples as the Nth descriptive text verification sample set.
[0142] In one specific implementation, the adaptation degree acquisition submodule includes:
[0143] 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.
[0144] The adaptation degree acquisition unit is used to input the normalized Nth quantity and Nth accuracy corresponding to each initial scene into the preset adaptation degree calculation model to obtain the Nth adaptation degree between each initial scene and the Nth vector transformation model. The Nth adaptation degree is inversely proportional to the corresponding normalized Nth quantity and directly proportional to the corresponding Nth accuracy.
[0145] In one specific implementation, the preset degree threshold includes a first degree threshold and a second degree threshold, and the adaptation result acquisition submodule includes:
[0146] The first adaptation result acquisition unit is used to determine that the adaptation result of 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.
[0147] The second adaptation result acquisition unit is used to determine that the adaptation result of the current initial scene is not suitable if the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the second degree threshold.
[0148] The third adaptation result acquisition unit is used to determine the adaptation result of the current initial scene as to be judged 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.
[0149] In one specific embodiment, the data acquisition module 21 further includes:
[0150] The second data acquisition submodule is used to acquire several preset tables, preset description text and preset scenarios corresponding to each preset table, and target vector transformation model corresponding to each preset scenario.
[0151] The consistency matching submodule is used to perform scenario consistency matching between the preset scenario corresponding to each preset table and the target scenario corresponding to the initial statement, and obtain the scenario consistency matching result between each preset table and the initial statement. The scenario consistency matching result includes scenario consistency and scenario inconsistency.
[0152] The intermediate table filtering submodule is used to determine each preset table whose scenario consistency matching result is consistent with the scenario as the intermediate table corresponding to the initial statement.
[0153] The intermediate description vector acquisition submodule is used to obtain the intermediate description vector corresponding to each intermediate table by transforming the target vector of the preset scenario corresponding to the preset description text of each intermediate table into a model.
[0154] The statement vector acquisition submodule is used to obtain the statement vector corresponding to the initial statement based on the target vector transformation model corresponding to the target scenario corresponding to the initial statement.
[0155] The intermediate matching degree acquisition submodule is used to obtain the intermediate matching degree between the initial statement and each intermediate table based on the intermediate description vector corresponding to each intermediate table and the statement vector corresponding to the initial statement.
[0156] The target table filtering submodule is used to filter out the target table corresponding to the initial statement from all intermediate tables based on the degree of intermediate matching between the initial statement and each intermediate table.
[0157] In one specific embodiment, the data acquisition module 21 further includes:
[0158] The third data acquisition submodule is used to acquire several historical statements and the preset scenarios corresponding to each historical statement.
[0159] The historical statement vector acquisition submodule is used to obtain the historical statement vector corresponding to each historical statement based on the target vector transformation model corresponding to the preset scenario for each historical statement.
[0160] In one specific embodiment, the matching result acquisition module 23 further includes:
[0161] The word consistency matching submodule is used to perform word consistency matching between the current target keyword and each custom keyword in the preset keyword configuration table for any target keyword, and obtain the word consistency matching results between the current target keyword and each custom keyword. The word consistency matching results include word consistency and word inconsistency.
[0162] The matching result acquisition submodule is used to determine the matching result of the current target keyword if the word consistency matching result between the current custom keyword and the current target keyword is consistent.
[0163] The keyword traversal submodule is used to traverse all custom keywords. If the word consistency matching results between the current target keyword and all custom keywords are inconsistent, then the empty set is determined as the matching result corresponding to the current target keyword.
[0164] It should be noted that the information interaction and execution process between the above modules are based on the same concept as the method embodiments of the present invention. For details on their specific functions and technical effects, please refer to the method embodiments section, which will not be repeated here.
[0165] Example 3
[0166] Embodiment 3 of the present invention provides a non-transitory computer-readable storage medium, which stores at least one instruction or at least one program segment, wherein the at least one instruction or at least one program segment is loaded and executed by a processor to implement the following steps:
[0167] S10: Obtain the historical input database, the initial statement input by the target user, the target scenario and target table corresponding to the initial statement, and the target vector transformation model and preset keyword configuration table corresponding to the target scenario. The historical input database includes several historical statements, the historical statement vector and historical SQL statement corresponding to each historical statement, and the preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0168] S20: Extract keywords from the initial statement to obtain several target keywords corresponding to the initial statement.
[0169] S30, match each target keyword with the preset keyword configuration table to obtain the matching result corresponding to each target keyword. The matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression.
[0170] S40: Based on the matching results of all target keywords, obtain several target regular expressions corresponding to the initial statement.
[0171] S50: Obtain the statement vector corresponding to the initial statement based on the target vector transformation model corresponding to the target scenario of the initial statement.
[0172] S60: Based on the statement vector and the historical statement vector corresponding to each historical statement, obtain the similarity between the initial statement and each historical statement.
[0173] S70, based on the comparison between the similarity between the initial statement and each historical statement and a preset similarity threshold, select several reference statements corresponding to the initial statement from all historical statements.
[0174] S80: Input the statement vector, target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0175] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include both non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and RAMbus dynamic RAM (RDRAM), etc.
[0176] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above 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.
[0177] Example 4
[0178] Embodiment 4 of the present invention provides an electronic device, which includes a processor and a non-transitory computer-readable storage medium as described in Embodiment 3 of the present invention.
[0179] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention in any way. Although the present invention has been disclosed above with reference to preferred embodiments, it is not intended to limit the present invention. Any person skilled in the art can make some modifications or alterations to the above-disclosed technical content to create equivalent embodiments without departing from the scope of the present invention. Any simple modifications, equivalent changes and alterations made to the above embodiments based on the technical essence of the present invention without departing from the scope of the present invention shall still fall within the scope of the present invention.
Claims
1. A method for generating SQL statements based on a large language model, characterized in that, The SQL statement generation method includes the following steps: S10: Obtain the historical input database, the initial statement input by the target user, the target scenario and target table corresponding to the initial statement, and the target vector transformation model and preset keyword configuration table corresponding to the target scenario. The historical input database includes several historical statements, a historical statement vector corresponding to each historical statement, and a historical SQL statement. The preset keyword configuration table includes several custom keywords and a regular expression corresponding to each custom keyword. S10 includes the following steps: S101, obtain the initial vector transformation model, description text samples corresponding to several preset tables, preset scenarios and description vector samples corresponding to each description text sample, and preset sampling ratios corresponding to each preset scenario; S102, set the preset sampling ratio as the target sampling ratio, set each preset scene as the initial scene, and initialize the number of repetitions N=1; S103, sample from the plurality of description text samples according to the 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; S104, based on 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, train the initial vector conversion model to obtain the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model; S105, based on the Nth quantity and Nth accuracy corresponding to each initial scene, obtain the Nth degree of fit between each initial scene and the Nth vector transformation model; S106, For any initial scene, determine the adaptation result corresponding to the current initial scene based on the comparison result between the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold, wherein the adaptation result includes adaptation, non-adaptation, and undetermined. S107, if the adaptation result corresponding to the current initial scene is an adaptation, then the current initial scene is determined as the first scene, and the Nth vector transformation model is determined as the target vector transformation model corresponding to the current first scene; S108, If the adaptation result corresponding to the current initial scene is not suitable, then the current initial scene is determined as the second scene; S109, If the adaptation result corresponding to the current initial scene is to be judged, then the current initial scene is determined as the third scene; S110, traverse all initial scenes, obtain all first scenes and the target vector transformation model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scenes; S111, based on the preset scenario and description vector sample corresponding to each description text sample, obtain the target description text sample set corresponding to each second scenario; S113, The initial vector conversion model is trained based on 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; S114, if the number of scenarios is greater than 0, then update the preset sampling ratio corresponding to each third scenario and determine the updated preset sampling ratio as the target sampling ratio, determine each third scenario as the initial scenario, update N=N+1, and repeat step S103 until the updated number of scenarios is equal to 0, and obtain the target vector conversion model corresponding to each preset scenario. S115, Based on the target vector transformation model corresponding to each preset scenario, obtain the target vector transformation model corresponding to the target scenario; S20, extract keywords from the initial statement to obtain several target keywords corresponding to the initial statement; S30, Match each target keyword with the preset keyword configuration table to obtain the matching result corresponding to each target keyword, wherein the matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression; S40, Based on the matching results of all target keywords, obtain several target regular expressions corresponding to the initial statement; S50, obtain the statement vector corresponding to the initial statement according to the target vector transformation model corresponding to the target scenario corresponding to the initial statement; S60, based on the statement vector and the historical statement vector corresponding to each historical statement, obtain the similarity between the initial statement and each historical statement; S70, based on the comparison between the similarity between the initial statement and each historical statement and a preset similarity threshold, select several reference statements corresponding to the initial statement from all historical statements; S80, input the statement vector, the target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and historical SQL statements corresponding to each reference statement into the preset large language model to obtain the SQL statement corresponding to the initial statement.
2. The SQL statement generation method based on a large language model according to claim 1, characterized in that, Each preset scenario corresponds to a preset sampling ratio, including a training sample sampling ratio and a validation sample sampling ratio. S103 includes the following steps: S1031, For any initial scene, according to the sampling ratio of the training samples corresponding to the current initial scene, sample a number of descriptive text samples from all descriptive text samples corresponding to the current initial scene as training samples, and obtain the Nth number of descriptive text samples used as training samples corresponding to the current initial scene. S1032, Based on the sampling ratio of the verification samples corresponding to the current initial scenario, sample a number of description text samples from all description text samples corresponding to the current initial scenario as verification samples; S1033, traverse all initial scenes and obtain several descriptive text samples as training samples and several descriptive text samples as verification samples corresponding to each initial scene. S1034, determine the set of several descriptive text samples corresponding to all initial scenes as training samples as the Nth descriptive text training sample set; S1035, determine the set of several descriptive text samples corresponding to all initial scenarios as verification samples as the Nth descriptive text verification sample set.
3. The SQL statement generation method based on a large language model according to claim 1, characterized in that, S105 includes the following steps: S1051, normalize the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene; S1052, input the normalized Nth quantity and Nth accuracy corresponding to each initial scene into the preset adaptation degree calculation model to obtain the Nth adaptation degree between each initial scene and the Nth vector transformation model, wherein the Nth adaptation degree is inversely proportional to the corresponding normalized Nth quantity and the Nth adaptation degree is directly proportional to the corresponding Nth accuracy.
4. The SQL statement generation method based on a large language model according to claim 1, characterized in that, The preset degree threshold includes a first degree threshold and a second degree threshold. 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, then determine that the adaptation result corresponding to the current initial scene is not suitable. S1062, If the Nth degree of adaptation corresponding to the current initial scene is greater than or equal to the second degree threshold, then it is determined 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, then the adaptation result corresponding to the current initial scene is determined to be pending judgment.
5. The SQL statement generation method based on a large language model according to claim 1, characterized in that, S10 also includes the following steps: S116, obtain several preset tables, preset description text and preset scenarios corresponding to each preset table, and target vector transformation model corresponding to each preset scenario; S117, Perform scenario consistency matching between the preset scenario corresponding to each preset table and the target scenario corresponding to the initial statement, and obtain the scenario consistency matching result between each preset table and the initial statement, wherein the scenario consistency matching result includes scenario consistency and scenario inconsistency; S118, each preset table whose scenario consistency matching result is consistent with the scenario is determined as the intermediate table corresponding to the initial statement; S119. Based on the target vector transformation model corresponding to the preset scenario corresponding to the preset description text of each intermediate table, obtain the intermediate description vector corresponding to each intermediate table. S120, obtain the statement vector corresponding to the initial statement according to the target vector transformation model corresponding to the target scenario corresponding to the initial statement; S121, based on the intermediate description vector corresponding to each intermediate table and the statement vector corresponding to the initial statement, the intermediate matching degree between the initial statement and each intermediate table is obtained; S122, Based on the degree of intermediate matching between the initial statement and each intermediate table, select the target table corresponding to the initial statement from all intermediate tables.
6. The SQL statement generation method based on a large language model according to claim 1, characterized in that, S10 also includes the following steps: S123, obtain several historical statements and the preset scenario corresponding to each historical statement; S124. Based on the target vector transformation model corresponding to the preset scenario for each historical statement, obtain the historical statement vector corresponding to each historical statement.
7. The SQL statement generation method based on a large language model according to claim 1, characterized in that, S30 also includes the following steps: S301, for any target keyword, perform word consistency matching between the current target keyword and each custom keyword in the preset keyword configuration table, and obtain the word consistency matching result between the current target keyword and each custom keyword, wherein the word consistency matching result includes word consistency and word inconsistency; S302, for any custom keyword, if the word consistency matching result between the current target keyword and the current custom keyword is consistent, then the set consisting of the current custom keyword and the regular expression corresponding to the current custom keyword is determined as the matching result corresponding to the current target keyword; S303: Iterate through all custom keywords. If the word consistency matching results between the current target keyword and all custom keywords are inconsistent, then the empty set is determined as the matching result corresponding to the current target keyword.
8. A non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores at least one instruction or at least one program segment, characterized in that, The at least one instruction or the at least one program segment is loaded and executed by the processor to implement the SQL statement generation method based on a large language model as described in any one of claims 1-7.
9. An electronic device, characterized in that, Includes a processor and the non-transitory computer-readable storage medium as described in claim 8.
Citation Information
Patent Citations
Ship social method and apparatus, electronic device and readable storage medium
CN109327510A
Method for automatically generating database query statement based on NLP language model
CN116991869A
Method, device, equipment and medium for converting natural language problem into SQL (Structured Query Language) statement
CN117667992A
Data preparation method, system and device for AIGC interaction analysis and medium
CN118093795A