Model-based sql code generation method and device, storage medium and equipment
Patent Information
- Application Number
- CN202511145387.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-15
- Publication Date
- 2026-10-23
- Estimated Expiration
- 2045-08-15
AI Technical Summary
[0005]本申请提供了一种基于模型的SQL代码生成方法、装置、存储介质及设备,用于解决模型在处理长文本或复杂逻辑时,容易出现信息丢失或语法错误,模型难以调试和优化的问题
[0014]根据本申请的第四方面,提供了一种计算机设备,所述计算机设备包括上述基于模型的SQL代码生成装置。
Smart Images

Figure CN121092567B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of deep learning technology, and in particular to a model-based SQL code generation method, apparatus, storage medium and device. Background Technology
[0002] Structured Query Language (SQL) is a database query and programming language used to access, query, update, and manage relational database systems. Wensheng SQL is a technology that translates natural language descriptions into SQL code. When users express their data query needs in everyday language, it can automatically parse and generate the corresponding SQL code.
[0003] In related technologies, deep learning-based methods are used to generate SQL code. Specifically, sequence-to-sequence (Seq2Seq) models, such as recurrent nearal networks (RNNs) and long short-term memory networks (LSTMs), are employed to directly generate SQL code in an end-to-end manner.
[0004] However, deep learning-based models are prone to information loss or grammatical errors when processing long texts or complex logic, resulting in poor model interpretability and difficulties in debugging and optimization. Summary of the Invention
[0005] This application provides a model-based SQL code generation method, apparatus, storage medium, and device to address the problems of information loss or syntax errors, and the difficulty in debugging and optimizing models when processing long texts or complex logic. The technical solution is as follows: According to a first aspect of this application, a model-based SQL code generation method is provided, the method comprising: Obtain user requirement information, which describes the functionality to be implemented by the SQL code to be generated; The semantic segmentation module in the analysis model is used to perform semantic segmentation on the requirement information to obtain multiple semantic segments. Each semantic segment corresponds to a requirement, and each requirement is associated with a maximum of three data tables. The intent recognition module and named entity recognition (NER) module in the analysis model are used to identify the requirement information to obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields. Each semantic fragment is combined with the key information to form a prompt word; For each prompt word, the fine-tuned SQL generation model is used to process the prompt word and the requirement information to obtain an SQL code segment; All prompt words, all SQL code segments, and the key information are combined to form a total prompt word. The total prompt word and the requirement information are then processed using a fine-tuned SQL generation model to obtain the first SQL code.
[0006] In one possible implementation, the method further includes: The first SQL code is corrected using an SQL checker to obtain the second SQL code; The second SQL code is corrected and its logic optimized using the fine-tuned SQL error correction model to obtain the third SQL code. The third SQL code and the key information are processed using an SQL interpretation model to obtain a code interpretation; Output the third SQL code and its explanation.
[0007] In one possible implementation, the step of using an SQL checker to correct the first SQL code to obtain the second SQL code includes: The SQL checker is used to check whether the key information in the first SQL code matches the associated data table. If the key information matches the associated data table, the SQL checker is used to read the syntax rules, and the first SQL code is syntax checked according to the syntax rules. If there is a syntax error in the first SQL code, the SQL checker is used to correct the syntax of the first SQL code to obtain the second SQL code.
[0008] In one possible implementation, the output of the third SQL code and the code explanation includes: Based on the code explanation, detect whether there are any errors in the third SQL code; If the third SQL code contains errors, the third SQL code and its interpretation shall be corrected according to the errors. Output the corrected third SQL code and its explanation.
[0009] In one possible implementation, the output of the third SQL code and the code explanation includes: Output an explanation of the code; Receive correction information from the user based on the code interpretation input; The third SQL code and its interpretation are corrected based on the correction information. Output the corrected third SQL code and its explanation.
[0010] In one possible implementation, the method further includes: The third SQL code is executed to obtain the execution result, which includes the fields filtered from the data table according to the filtering conditions. Output the results of the operation.
[0011] In one possible implementation, the method further includes: When the data table is a file, the third SQL code and the key information are converted into Python code.
[0012] According to a second aspect of this application, a model-based SQL code generation apparatus is provided, the apparatus comprising: The acquisition module is used to acquire user requirement information, which describes the function to be implemented by the SQL code to be generated; The segmentation module is used to perform semantic segmentation on the requirement information using the semantic segmentation module in the analysis model, resulting in multiple semantic segments. Each semantic segment corresponds to a requirement, and each requirement is associated with a maximum of three data tables. The identification module is used to identify the requirement information using the intent identification module and the named entity recognition (NER) module in the analysis model, and obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields. The component module is used to combine each semantic fragment with the key information to form a prompt word; The generation module is used to process each prompt word and the requirement information using a fine-tuned SQL generation model to obtain an SQL code segment. The generation module is further configured to combine all prompt words, all SQL code segments, and the key information into a total prompt word, and process the total prompt word and the requirement information using a fine-tuned SQL generation model to obtain the first SQL code.
[0013] According to a third aspect of this application, a computer-readable storage medium is provided, wherein at least one instruction is stored therein, the at least one instruction being loaded and executed by a processor to implement the model-based SQL code generation method as described above.
[0014] According to a fourth aspect of this application, a computer device is provided, the computer device including the above-described model-based SQL code generation apparatus.
[0015] The beneficial effects of the technical solution provided in this application include at least the following: By using a semantic segmentation model, requirement information is divided into multiple semantic fragments, with each fragment corresponding to a specific requirement and each requirement associated with a maximum of three data tables. Each semantic fragment is then combined with key information to form a prompt word. A fine-tuned SQL generation model is then used to process each prompt word and requirement information to obtain an SQL code segment. Finally, all prompt words, SQL code segments, and key information are combined to form a final prompt word. The fine-tuned SQL generation model is then used to process the final prompt word and requirement information to obtain the first SQL code. This approach allows for a deep understanding of complex user requirements, breaking them down into multiple simpler sub-requirements. These sub-requirements are then gradually implemented through multiple concise SQL code segments, simplifying and improving code accuracy, shortening model fine-tuning cycles, reducing costs, and facilitating user comprehension.
[0016] By using SQL checkers and SQL error correction models to correct and optimize SQL code, the accuracy of the code can be further improved. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in the embodiments of this application, 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 this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a flowchart of a model-based SQL code generation method provided in one embodiment of this application; Figure 2 This is a flowchart of a model-based SQL code generation method provided in one embodiment of this application; Figure 3 This is a structural block diagram of a model-based SQL code generation apparatus provided in one embodiment of this application. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.
[0020] like Figure 1 The diagram illustrates a flowchart of a model-based SQL code generation method according to an embodiment of this application. This model-based SQL code generation method can be applied to computer devices. The model-based SQL code generation method may include: Step 101: Obtain the user's requirements information, which describes the functionality to be implemented by the SQL code to be generated.
[0021] The requirement information is a textual description of the function that the user wants to implement using SQL code. It can be a piece of text entered by the user or a piece of text extracted from multiple rounds of user conversation. This embodiment does not limit the method of obtaining the requirement information.
[0022] In one example, the requirement is to filter data from a data table. Assuming three tables are involved: Table 1 stores keyword information, Table 2 stores the algorithm configuration information for the keywords, and Table 3 stores the effective websites for the keywords. The user's requirement could be: "Retrieve a list of records that are only effective on websites with the website code 'abc1234', and whose keyword detection algorithm is set to '1'. The fields to retain are: keyword, algorithm, effective time, and user who created it."
[0023] Step 102: Use the semantic segmentation module in the analysis model to perform semantic segmentation on the requirement information to obtain multiple semantic segments. Each semantic segment corresponds to a requirement, and each requirement is associated with a maximum of three data tables.
[0024] An analysis model is a model for analyzing demand information, and includes at least a semantic segmentation module, an intent recognition module, and a named entity recognition (NER) module.
[0025] Semantic segmentation is a concept in Natural Language Processing (NLP). Its core objective is to segment a text, ensuring that each segment is semantically independent and complete. Unlike traditional rule-based text segmentation, semantic segmentation focuses more on the actual meaning and logical structure of the text content.
[0026] Intent recognition is a core task in NLP, aiming to extract users' underlying purposes or needs from their text. It is the foundation for understanding user intent and providing corresponding services.
[0027] NER aims to extract the location and category of entities from text. Common entity categories include person names (PER), place names (LOC), organization names (ORG), and time (TIME).
[0028] Requirements information typically corresponds to complex needs. By using a semantic segmentation module to divide the requirements information into multiple semantic fragments, each fragment corresponds to a simpler, smaller requirement, thus breaking down complex requirements into simpler, smaller ones. Then, a short SQL code snippet can be generated based on each smaller requirement to limit code complexity and progressively implement the customer's needs.
[0029] Taking the requirement information above as an example, after semantic segmentation, three semantic fragments can be obtained. Semantic fragment a is "the central word that is effective when the website code is abc1234", which is associated with data tables 1 and 3; semantic fragment b is "the detection algorithm configured for the central word is 1", which is associated with data tables 1 and 2; and semantic fragment c is "reserved fields: central word, algorithm, effective time, user who created, etc.", which is associated with data table 1.
[0030] Step 103: Use the intent recognition module and NER module in the analysis model to identify the demand information and obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields.
[0031] Taking the requirements information above as an example, the key information includes: (1) Table names, field names, primary keys, foreign keys, and table descriptions of data tables 1-3. (2) Screening criteria (3) Reserved fields: core keywords, algorithm, effective time, user who created the application, etc. (4) The output is a list of data.
[0032] Step 104: Combine each semantic fragment with key information to form a prompt word.
[0033] (1) The prompts for the first requirement include: The table information related to the requirements is as follows: Table 3, Information on websites that have been activated for the central keyword: Central keyword ID, Activated website The fields that need to be output are: keyword ID, and the website from which the keyword applies. The filtering criteria are: The website's core keywords are encoded as abc1234. Generate SQL1 (2) The prompts for the second requirement include: The table information related to the requirements is as follows: Table 2 shows the algorithm configuration information for the central keyword: central keyword ID, algorithm, effective time, and expiration time. The fields to be output are: the core keyword ID, and the algorithm. The filtering criteria are: The detection algorithm for the center word configuration is 1. Generate SQL2 Step 105: For each prompt word, the fine-tuned SQL generation model is used to process the prompt word and the requirement information to obtain an SQL code segment.
[0034] Before using the fine-tuned SQL generation model, it needs to be fine-tuned using samples. Specifically, each sample includes prompt words, requirement information, and a real SQL code snippet. During fine-tuning, the SQL generation model generates a predicted SQL code snippet based on the prompt words and requirement information. The loss value is calculated based on the predicted SQL code snippet and the real SQL code snippet. The weights of the SQL generation model are updated based on this loss value until the SQL generation model meets preset conditions, at which point fine-tuning stops.
[0035] For each prompt word, the prompt word can be concatenated with the requirement information. The concatenated data is then input into the fine-tuned SQL generation model, which processes the data and outputs an SQL code snippet.
[0036] Step 106: Combine all prompts, all SQL code segments, and key information into a total prompt. Use the fine-tuned SQL generation model to process the total prompt and requirement information to obtain the first SQL code.
[0037] The main keywords include: The table information related to the requirements is as follows: Table 1 contains key keyword information, including the fields: id, keyword. Table 2 shows the algorithm configuration information for the central keyword: central keyword ID, algorithm, effective time, and expiration time. Table 3, Information on websites that have been activated for the central keyword: Central keyword ID, Activated website The fields that need to be output are: core keywords, algorithm, effective time, and user who created the application. Semantic segmentation and filtering criteria: a. Website code abc1234 is effective for the core keywords. Generated SQL fragment: SQL1 b. The detection algorithm for the central word configuration is 1. SQL snippet generated: SQL2 Based on the filtering criteria and the generated SQL fragments, generate the final SQL, and generate temporary tables as needed. Ensure that the statements are simple and clear. When performing data table joins, do not use too many data table joins, and limit them to no more than 3 data tables.
[0038] Based on the above requirements and information, SQL is generated step by step, and the generation process is recorded.
[0039] The fine-tuned SQL generation model can use the above general prompts and requirement information to generate the final first SQL code.
[0040] In summary, the model-based SQL code generation method provided in this application segmentes requirement information into multiple semantic fragments using a semantic segmentation model. Each semantic fragment corresponds to a requirement, and each requirement is associated with a maximum of three data tables. Then, each semantic fragment and key information are combined to form a prompt word. The fine-tuned SQL generation model is then used to process each prompt word and requirement information to obtain an SQL code segment. Finally, all prompt words, all SQL code segments, and key information are combined to form a total prompt word. The fine-tuned SQL generation model is then used to process the total prompt word and requirement information to obtain the first SQL code. In this way, after a deep understanding of the user's complex requirements, the complex requirements can be broken down into multiple simpler requirements. The user's requirements can then be gradually realized through multiple short SQL code segments. This simplifies the code, improves its accuracy, shortens the model fine-tuning cycle, reduces costs, and makes it easier for users to understand.
[0041] After obtaining the first SQL code, it can be corrected to improve its accuracy, and it can also be explained to improve its readability.
[0042] like Figure 2 The diagram illustrates a flowchart of a model-based SQL code generation method according to an embodiment of this application. This model-based SQL code generation method can be applied to computer devices. The model-based SQL code generation method may include: Step 201: Use the SQL checker to correct the first SQL code to obtain the second SQL code.
[0043] Specifically, the SQL checker is used to correct the first SQL code to obtain the second SQL code, which may include: (1) Use the SQL checker to check whether the key information in the first SQL code matches the associated data table.
[0044] Specifically, the SQL checker reads the identifier and filter fields of the data table from the key information, checks whether the identifier of the associated data table is the same as the read identifier, and checks whether the associated data table contains the filter fields. If the identifiers are the same and the associated data table contains the filter fields, then the key information is determined to match the associated data table; if the identifiers are different and / or the associated data table does not contain the filter fields, then the key information is determined to not match the associated data table, and the SQL code is regenerated.
[0045] (2) If the key information matches the associated data table, the SQL checker is used to read the syntax rules and the first SQL code is checked according to the syntax rules.
[0046] The syntax rules can be pre-stored in the database, and the SQL checker can directly read the syntax rules from the database.
[0047] (3) If there is a syntax error in the first SQL code, the SQL checker is used to correct the syntax of the first SQL code to obtain the second SQL code.
[0048] If there are no syntax errors in the first SQL code, then the first SQL code is identified as the corrected second SQL code, and step 203 is executed.
[0049] Taking the example above as an example, let's assume the first SQL code is as follows: SQL1: select the keyword id from table3 where sitecode='abc1234' SQL2: select core_word_id, detect_algorithm from table2 where detect_algorithm='1' SQL3: select id, core keyword, effective time, created user from table1 After syntax correction using the SQL checker, the second SQL code is as follows: SQL1: create table tmp1 as Select id, keyword, effective time, and created user. fraom Table 1 a left join table2 b on a.id=b.center_word_id where b.detect_algorithm='1' SQL2: select a.* from tmp1 a left join table3 on a.id=b.center_word_id where b.sitecode='abc1234' Step 202: Use the fine-tuned SQL error correction model to correct errors and optimize the logic of the second SQL code to obtain the third SQL code.
[0050] Before using the fine-tuned SQL error correction model, it needs to be fine-tuned using samples. Specifically, each sample includes the SQL code segment before optimization and the SQL code segment after manual optimization. During fine-tuning, the SQL error correction model optimizes the SQL code segment before optimization, calculates the loss value based on the SQL code segment after model optimization and the SQL code segment after manual optimization, and updates the weights of the SQL error correction model based on the loss value until the SQL error correction model meets the preset conditions, at which point fine-tuning stops.
[0051] The SQL error correction model can perform code inspection on the second SQL statement and correct any errors. For example, if "fraom" is misspelled in SQL1, it can be changed to "from".
[0052] The SQL error correction model can logically optimize the second SQL code. For example, the join method between SQL1 and SQL2 should not be "left", but should be optimized to "inner"; according to the contents of data tables 1-3, it is more reasonable to join the intermediate result generated by SQL1 with data table 3.
[0053] After error correction and logic optimization, the third SQL code is as follows: SQL1: create table tmp1 as Select id, keyword, effective time, and created user. from Table 1 a inner join Table 3 b on a.id=b.centerwordid where b.sitecode='abc1234' SQL2: select a.* from tmp1 a Join table2 on a.id=b.center_word_id where b.detect_algorithm='1' Step 203: Use the SQL interpretation model to process the third SQL code and key information to obtain the code interpretation.
[0054] Step 204: Output the third SQL code and its explanation.
[0055] In the first implementation, outputting the third SQL code and its explanation may include: detecting whether there are errors in the third SQL code based on the explanation; if there are errors in the third SQL code, correcting the third SQL code and its explanation based on the errors; and outputting the corrected third SQL code and its explanation.
[0056] In simple terms, the SQL interpretation model can automatically detect errors in the code interpretation. If there are unreasonable errors in the code interpretation, the third-party SQL code and code interpretation can be modified accordingly.
[0057] In the second implementation, outputting the third SQL code and code explanation may include: outputting the code explanation; receiving correction information input by the user based on the code explanation; correcting the third SQL code and code explanation according to the correction information; and outputting the corrected third SQL code and code explanation.
[0058] In simple terms, users can view the code explanation. If a user believes there are unreasonable errors in the code explanation, they can input correction information, and the SQL explanation model will modify the third-party SQL code and code explanation based on the correction information.
[0059] In this embodiment, the third-party SQL code can be optimized using either of the two implementation methods described above, or a combination of the two methods. For example, the SQL interpretation model first optimizes the third-party SQL code using the first implementation method, and then, after confirming that there are no problems, optimizes it using the second implementation method.
[0060] In this embodiment, the third-party SQL code and its explanation can be provided to the user, or the third-party SQL code, its explanation, and the execution result of the third-party SQL code can be provided to the user. Specifically, the method further includes: running the third-party SQL code, obtaining the execution result, which includes fields filtered from the data table according to the filtering conditions; and outputting the execution result. For example, the execution result may be a list of data containing fields such as core keywords, algorithm, effective time, and user who created the code.
[0061] In this embodiment, when the data table is a table in a database, no format conversion is required; when the data table is a file, it is also necessary to convert the third-party SQL code and key information into Python code. That is, the third-party SQL code and key information are converted into corresponding Python code that uses pandas and numpy to process the data.
[0062] In summary, the model-based SQL code generation method provided in this application can further improve the accuracy of the code by using an SQL checker and an SQL error correction model to correct and optimize the SQL code.
[0063] like Figure 3 The diagram illustrates a structural block diagram of a model-based SQL code generation apparatus according to an embodiment of this application. This model-based SQL code generation apparatus can be applied to a computer device. The apparatus includes: The acquisition module 310 is used to acquire user requirement information, which describes the function to be implemented by the SQL code to be generated; The segmentation module 320 is used to perform semantic segmentation on the demand information using the semantic segmentation module in the analysis model, resulting in multiple semantic segments. Each semantic segment corresponds to a demand, and each demand is associated with a maximum of three data tables. The identification module 330 is used to identify the requirement information using the intent identification module and the named entity recognition (NER) module in the analysis model, and obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields. Module 340 is used to combine each semantic fragment with key information to form a prompt word; The generation module 350 is used to process the prompt words and requirement information using a fine-tuned SQL generation model for each prompt word, and obtain an SQL code segment. The generation module 350 is also used to compose a total prompt word from all prompt words, all SQL code segments and key information, and to process the total prompt word and requirement information using the fine-tuned SQL generation model to obtain the first SQL code.
[0064] In an optional embodiment, the generation module 350 is further configured to: The first SQL code is corrected using an SQL checker to obtain the second SQL code; The second SQL code is corrected and its logic optimized using a fine-tuned SQL error correction model to obtain the third SQL code. The SQL interpretation model is used to process the third-party SQL code and key information to obtain code interpretation; Output the third SQL code and its explanation.
[0065] In an optional embodiment, the generation module 350 is further configured to: Use an SQL inspector to check whether the key information in the first SQL code matches the associated data table; If the key information matches the associated data table, the SQL checker is used to read the syntax rules and perform a syntax check on the first SQL code according to the syntax rules. If there are syntax errors in the first SQL code, the SQL checker will be used to correct the syntax of the first SQL code to obtain the second SQL code.
[0066] In an optional embodiment, the generation module 350 is further configured to: Detect whether there are errors in the third-party SQL code based on the code explanation; If the third SQL code contains errors, the third SQL code and its explanation will be corrected according to the errors. Output the corrected third SQL code and its explanation.
[0067] In an optional embodiment, the generation module 350 is further configured to: Output code explanation; Receive correction information from the user based on code interpretation; The third SQL code and its explanation have been corrected based on the correction information; Output the corrected third SQL code and its explanation.
[0068] In an optional embodiment, the generation module 350 is further configured to: Run the third SQL code to get the results, which include the fields filtered from the data table based on the filter conditions. Output the results.
[0069] In an optional embodiment, the generation module 350 is further configured to: When the data table is a file, convert the third-party SQL code and key information into Python code.
[0070] In summary, the model-based SQL code generation device provided in this application segmentation model divides requirement information into multiple semantic fragments, with each semantic fragment corresponding to a requirement and each requirement associated with a maximum of three data tables. Then, each semantic fragment is combined with key information to form a prompt word. A fine-tuned SQL generation model is then used to process each prompt word and requirement information to obtain an SQL code segment. Finally, all prompt words, all SQL code segments, and key information are combined to form a total prompt word. The fine-tuned SQL generation model is then used to process the total prompt word and requirement information to obtain the first SQL code. This approach allows for a deep understanding of complex user requirements, breaking them down into multiple simpler requirements. These requirements are then gradually fulfilled through multiple concise SQL code segments, simplifying the code, improving its accuracy, shortening the model's fine-tuning cycle, reducing costs, and facilitating user comprehension.
[0071] By using SQL checkers and SQL error correction models to correct and optimize SQL code, the accuracy of the code can be further improved.
[0072] One embodiment of this application provides a computer-readable storage medium storing at least one instruction, which is loaded and executed by a processor to implement the model-based SQL code generation method described above.
[0073] One embodiment of this application provides a computer device, which includes any of the above-described model-based SQL code generation devices.
[0074] It should be noted that the model-based SQL code generation device provided in the above embodiments is only illustrated by the division of the above functional modules when performing model-based SQL code generation. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the model-based SQL code generation device can be divided into different functional modules to complete all or part of the functions described above. In addition, the model-based SQL code generation device and the model-based SQL code generation method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process is detailed in the method embodiments, which will not be repeated here.
[0075] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.
[0076] The above description is not intended to limit the embodiments of this application. Any adjustments, equivalent substitutions, improvements, etc., made within the spirit and principles of the embodiments of this application should be included within the protection scope of the embodiments of this application.
Claims
1. A model-based SQL code generation method, characterized in that, The method includes: Obtain user requirement information, which describes the functionality to be implemented by the SQL code to be generated; The semantic segmentation module in the analysis model is used to perform semantic segmentation on the requirement information to obtain multiple semantic segments. Each semantic segment corresponds to a requirement, and each requirement is associated with a maximum of three data tables. The intent recognition module and named entity recognition (NER) module in the analysis model are used to identify the requirement information to obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields. Each semantic fragment is combined with the key information to form a prompt word; For each prompt word, the prompt word is concatenated with the requirement information, and the concatenated data is input into the fine-tuned SQL generation model so that the fine-tuned SQL generation model processes the data and outputs an SQL code segment; All prompt words, all SQL code segments, and the key information are combined to form a total prompt word. The total prompt word and the requirement information are then processed using a fine-tuned SQL generation model to obtain the first SQL code. The method further includes: correcting the first SQL code using an SQL checker to obtain the second SQL code; correcting and optimizing the second SQL code using a fine-tuned SQL error correction model to obtain the third SQL code; processing the third SQL code and the key information using an SQL interpretation model to obtain the code interpretation; and outputting the third SQL code and the code interpretation. The step of outputting the third SQL code and the code explanation includes: detecting whether there are errors in the third SQL code based on the code explanation; if there are errors in the third SQL code, correcting the third SQL code and the code explanation based on the errors; and outputting the corrected third SQL code and code explanation.
2. The model-based SQL code generation method according to claim 1, characterized in that, The step of using an SQL checker to correct the first SQL code to obtain the second SQL code includes: The SQL checker is used to check whether the key information in the first SQL code matches the associated data table. If the key information matches the associated data table, the SQL checker is used to read the syntax rules, and the first SQL code is syntax checked according to the syntax rules. If there is a syntax error in the first SQL code, the SQL checker is used to correct the syntax of the first SQL code to obtain the second SQL code.
3. The model-based SQL code generation method according to claim 1, characterized in that, The output of the third SQL code and the code explanation includes: Output an explanation of the code; Receive correction information from the user based on the code interpretation input; The third SQL code and its interpretation are corrected based on the correction information. Output the corrected third SQL code and its explanation.
4. The model-based SQL code generation method according to claim 1, characterized in that, The method further includes: The third SQL code is executed to obtain the execution result, which includes the fields filtered from the data table according to the filtering conditions. Output the results of the operation.
5. The model-based SQL code generation method according to claim 1, characterized in that, The method further includes: When the data table is a file, the third SQL code and the key information are converted into Python code.
6. A model-based SQL code generation device, characterized in that, The device includes: The acquisition module is used to acquire user requirement information, which describes the function to be implemented by the SQL code to be generated; The segmentation module is used to perform semantic segmentation on the requirement information using the semantic segmentation module in the analysis model, resulting in multiple semantic segments. Each semantic segment corresponds to a requirement, and each requirement is associated with a maximum of three data tables. The identification module is used to identify the requirement information using the intent identification module and the named entity recognition (NER) module in the analysis model, and obtain key information. The key information includes at least the identifier of the data table to be processed, the filtering conditions for the fields in the data table, the fields filtered according to the filtering conditions, and the output type of the fields. The component module is used to combine each semantic fragment with the key information to form a prompt word; The generation module is used to concatenate the prompt word with the requirement information for each prompt word, input the concatenated data into the fine-tuned SQL generation model, so that the fine-tuned SQL generation model can process the data and output an SQL code segment; The generation module is also used to compose a total prompt word from all prompt words, all SQL code segments and the key information, and to process the total prompt word and the requirement information using the fine-tuned SQL generation model to obtain the first SQL code; The generation module is further configured to: correct the first SQL code using an SQL checker to obtain the second SQL code; correct and optimize the second SQL code using a fine-tuned SQL error correction model to obtain the third SQL code; process the third SQL code and the key information using an SQL interpretation model to obtain the code interpretation; and output the third SQL code and the code interpretation. The generation module is further configured to: detect whether there are errors in the third SQL code based on the code explanation; if there are errors in the third SQL code, correct the third SQL code and the code explanation based on the errors; and output the corrected third SQL code and code explanation.
7. A computer-readable storage medium, characterized in that, The storage medium stores at least one instruction, which is loaded and executed by a processor to implement the model-based SQL code generation method as described in any one of claims 1 to 5.
8. A computer device, characterized in that, The computer device includes: the model-based SQL code generation apparatus of claim 6.
Citation Information
Patent Citations
SQL (Structured Query Language) generation and error correction method based on multi-stage feedback
CN119474129A
Data processing method and equipment
CN119918667A
